package cate.game.activity.accumulatedaypay;

import cate.common.table.activity.accumulatedaypay.AccumulateDayPayRwdRow;
import cate.common.table.activity.accumulatedaypay.AccumulatePayDayBaseRow;
import cate.common.table.d.GDOperation;
import cate.game.activity.accumulatedaypay.msg.AccumulateDayPayDataResp;
import cate.game.activity.base.core.RoleActivitySingle;
import cate.game.mail.po.MailBuilder;
import cate.game.res.MixRes;
import cate.game.res.MixResAdder;
import cate.game.role.Role;
import cate.game.role.mail.Mail;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcResult;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@NoteClass("玩家单个累天充值活动数据")
public class RoleAccumulateDayPay extends RoleActivitySingle {

	@NoteField("今日是否领取免费奖励")
	public boolean gotFree;

	@NoteField("今日充值已达标")
	public boolean doneToday;

	@NoteField("今日充值金额")
	public long payToday;

	@NoteField("累积充值天数")
	public int payDay;
	@NoteField(value = "领过的奖励",detail = "记录的是天数")
	public Set<Integer> rwdGot;

	@NoteField("支付记录")
	public List<PayRecord> payRecords;

	@Override
	public void initialize(Role role) {
		super.initialize(role);
		if (rwdGot == null) {
			rwdGot = new HashSet<>();
		}
		if (payRecords == null) {
			payRecords = new ArrayList<>();
		}
		role.getActivity().registerRecharge(this, this::onPay);
	}

	@Override
	public void onDaySpan(boolean silence) {
		super.onDaySpan(silence);
		gotFree = false;
		doneToday = false;
		payToday = 0;
		if(!silence){
			noticeUpdate();
		}
	}


	@Override
	public void onRoleEnd() {
		sendRwdMail();
		gotFree = false;
		doneToday = false;
		payToday = 0;
		payDay = 0;
		rwdGot.clear();
		payRecords.clear();
	}

	private void sendRwdMail(){
		AccumulatePayDayBaseRow baseRow = getRow();
		int mailTid = baseRow.mailTid;
		if(mailTid<=0){
			return;
		}
		int rwdGroup = baseRow.group;
		MixRes totalRwd = new MixRes();
		for (int day = 1; day <= payDay; day++) {
			if(rwdGot.contains(day)){
				continue;
			}
			AccumulateDayPayRwdRow rwdRow = getRole().getGame().table.accumulateDayPay.rwd.getRwdRow(rwdGroup, day);
			if(rwdRow==null){
				continue;
			}
			totalRwd.addList(new MixRes(rwdRow.rwdStr));
		}
		if(totalRwd.isEmpty()){
			return;
		}
		Mail mail = new MailBuilder().setReward(totalRwd).setContentId(mailTid).setOperation(GDOperation.ACCUMULATE_DAY_PAY).build();
		role.getMail().add(mail);
	}

	public void onPay(long payNum) {
		final long payAmount = payNum / 100;
		payToday += payAmount;
		payRecords.add(new PayRecord(payAmount));
		if (!doneToday) {
			int rwdGroup = getRow().group;
			AccumulateDayPayRwdRow rwdRow = getRole().getGame().table.accumulateDayPay.rwd.getRwdRow(rwdGroup, payDay + 1);
			if (rwdRow == null) {
				return;
			}
			if (payToday >= rwdRow.payNum) {
				doneToday = true;
				payDay++;
			}
		}
		noticeUpdate();
	}

	@JsonIgnore
	public EcResult<Void> getFreeRwd(){
		EcResult<Void> r = new EcResult<>();
		if(gotFree){
			return r.fail("奖励已领取");
		}
		gotFree = true;
		new MixResAdder().setRes(getRow().freeRwdStr).setOperation(GDOperation.ACCUMULATE_DAY_PAY_FREE).setDialogReward(true).exe(role);
		noticeUpdate();
		return r.success();
	}

	@JsonIgnore
	public EcResult<Void> getPayRwd(int day) {
		EcResult<Void> r = new EcResult<>();
		if (rwdGot.contains(day)) {
			return r.fail("奖励已领取");
		}
		if (day > payDay) {
			return r.fail("累充天数不足");
		}
		AccumulateDayPayRwdRow rwdRow = getRole().getGame().table.accumulateDayPay.rwd.getRwdRow(getRow().group, day);
		if (rwdRow == null) {
			return r.fail("奖励不存在");
		}
		rwdGot.add(day);
		new MixResAdder().setRes(rwdRow.rwdStr).setOperation(GDOperation.ACCUMULATE_DAY_PAY).setOperationSub(day).setDialogReward(true).exe(role);
		noticeUpdate();
		return r.success();
	}

	@Override
	public void initSend() {
		noticeUpdate();
	}

	@JsonIgnore
	public AccumulatePayDayBaseRow getRow() {
		return getRole().getGame().table.accumulateDayPay.base.get(base.configTid);
	}

	public void noticeUpdate() {
		role.sendNow(new AccumulateDayPayDataResp(this));
	}

	public static class PayRecord {

		@NoteField("支付时间")
		public long time;

		@NoteField("金额")
		public long amount;

		public PayRecord() {
		}

		public PayRecord(long amount) {
			this.time = System.currentTimeMillis();
			this.amount = amount;
		}
	}
}
